<?php
include_once("ConnectionManager.php");

function getAllBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();   
}

function getAllLeftBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B WHERE BANNER_TYPE = 1 ORDER BY EVENT_TITLE";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();   
}

function getAllCenterBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B WHERE BANNER_TYPE = 2 ORDER BY EVENT_TITLE";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();   
}

function getAllRightBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B WHERE BANNER_TYPE = 3 ORDER BY EVENT_TITLE";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();   
}

function getAllEventBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B WHERE BANNER_TYPE = 4 ORDER BY EVENT_TITLE";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();   
}

function getBanner($banner_id){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B WHERE BANNER_ID = $banner_id";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $banner = mysql_fetch_array($result);
    return isset($banner)?$banner:null;
}

function getActiveLeftBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B
                WHERE BANNER_TYPE = 1 AND BANNER_STATUS = 1";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return isset($result)?mysql_fetch_array($result):null;
}

function getActiveCenterBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B
                WHERE BANNER_TYPE = 2 AND BANNER_STATUS = 1";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return isset($result)?mysql_fetch_array($result):null;
}

function getActiveRightBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B
                WHERE BANNER_TYPE = 3 AND BANNER_STATUS = 1";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return isset($result)?mysql_fetch_array($result):null;
}

function getActiveEventBanner(){
    $query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID, (SELECT ART_TITLE FROM MITI_ARTICLE WHERE ART_ID = B.BANNER_EVE_ID) AS EVENT_TITLE FROM MITI_BANNER B 
                WHERE BANNER_TYPE = 4 AND BANNER_STATUS = 1";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return isset($result)?mysql_fetch_array($result):null;
}

function createBanner($type, $article, $img){
    $query = "INSERT INTO MITI_BANNER(BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_EVE_ID) VALUES('$img',$type, 0, $article)";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function updateBanner($type, $article, $img, $banner_id){
    $query = "UPDATE MITI_BANNER SET BANNER_IMG_PATH='$img', BANNER_TYPE = '$type', BANNER_EVE_ID=$article WHERE BANNER_ID = $banner_id";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function deleteBanner($banner_id){
    $query = "DELETE FROM MITI_BANNER WHERE BANNER_ID = $banner_id";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function enableLeftBanner($banner_id){
    $query = "UPDATE MITI_BANNER SET BANNER_STATUS = 0 WHERE BANNER_TYPE = 1";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    $query2 = "UPDATE MITI_BANNER SET BANNER_STATUS = 1 WHERE BANNER_ID = $banner_id";
    ConnectionManager::getInstance()->setQuery($query2);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function enableCenterBanner($banner_id){
    $query = "UPDATE MITI_BANNER SET BANNER_STATUS = 0 WHERE BANNER_TYPE = 2";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    $query2 = "UPDATE MITI_BANNER SET BANNER_STATUS = 1 WHERE BANNER_ID = $banner_id";
    ConnectionManager::getInstance()->setQuery($query2);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function enableRightBanner($banner_id){
    $query = "UPDATE MITI_BANNER SET BANNER_STATUS = 0 WHERE BANNER_TYPE = 3";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    $query2 = "UPDATE MITI_BANNER SET BANNER_STATUS = 1 WHERE BANNER_ID = $banner_id";
    ConnectionManager::getInstance()->setQuery($query2);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function enableEventBanner($banner_id){
    $query = "UPDATE MITI_BANNER SET BANNER_STATUS = 0 WHERE BANNER_TYPE = 4";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    $query2 = "UPDATE MITI_BANNER SET BANNER_STATUS = 1 WHERE BANNER_ID = $banner_id";
    echo $query;
    echo $query2;
    ConnectionManager::getInstance()->setQuery($query2);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

// mode = 1: find all
// mode = 2: find active
function findAllNewProductBanners($mode){
	$query = "SELECT BANNER_ID, BANNER_TYPE, BANNER_STATUS, BANNER_PRO_ID, BANNER_IMG_PATH,
					(SELECT CONCAT(PRO_ID, ',', PRO_CODE, ',', PRO_NAME) FROM MITI_PRODUCT WHERE PRO_ID = BANNER_PRO_ID) AS PRODUCT
	          FROM MITI_BANNER
			  WHERE BANNER_TYPE = 5";
	if($mode == 2) $query .= " AND BANNER_STATUS = 1";
	ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function createNewProductBanner($imagePath, $type, $status, $proId){
	$query = "INSERT INTO MITI_BANNER (BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_PRO_ID) VALUES ('$imagePath', $type, $status, $proId)";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_insert_id();
}

function findNewProductBannerBy($bannerId){
	$query = "SELECT BANNER_ID, BANNER_IMG_PATH, BANNER_TYPE, BANNER_STATUS, BANNER_PRO_ID, (SELECT CAT_ID FROM MITI_PRODUCT WHERE PRO_ID = BANNER_PRO_ID) AS CAT_ID 
			  FROM MITI_BANNER WHERE BANNER_ID = $bannerId";
	ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
	return $row;
}

function updateNewProductBanner($imagePath, $type, $status, $proId, $bannerId){
	$query = "UPDATE MITI_BANNER SET BANNER_IMG_PATH = '$imagePath', BANNER_TYPE = $type, BANNER_STATUS = $status, BANNER_PRO_ID = $proId WHERE BANNER_ID = $bannerId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_affected_rows() > 0;
}

function updateNewProductBannerStatus($bannerId, $status){
	$query = "UPDATE MITI_BANNER SET BANNER_STATUS = $status, BANNER_PRO_ID = $proId WHERE BANNER_ID = $bannerId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_affected_rows() > 0;
}
?>
